import {Body, Controller, Get, Post, Query, UseGuards, ForbiddenException, Put} from '@nestjs/common';
import {UserService} from './user.service';
import {userDto} from './dto/user.dto';
import {AuthGuard} from '@nestjs/passport';
import {Web3Service} from 'src/web3/web3.service';
import {userdetail} from './dto/userdetail.dto';

@UseGuards(AuthGuard('jwt'))
@Controller('user')
export class UserController {
    constructor(private UserService: UserService,
                private web3: Web3Service
    ) {
    }

    //获得用户基本信息
    // @UseGuards(AuthGuard('jwt'))
    @Get('/info')
    async getUser(@Query('username') username: string) {
        const user = await this.UserService.findOne(username);
        if (!user) {
            throw new ForbiddenException('用户不存在');
        }

        const res: any = await this.UserService.getUserInfo(user.address);
        res.hashAddress = user.address;
        return {
            statusCode: 200,
            res
        }
    }

    //实际上要做分页，未做，产品列表也是
    @Get('/all')
    async getAllUser() {
        const userList = await this.UserService.getAllUser();
        return {
            statusCode: 200,
            userList
        }
    }

    //一些set,get方法，直接写在controller

    @Get('/is-enable')
    async getUserEnable(@Query('username') username: string) {
        const user = await this.UserService.findOne(username);
        if (!user) {
            throw new ForbiddenException('用户不存在');
        }
        const isenable = await this.web3.getUserEnable(user.address);
        return {
            statusCode: 200,
            res: {isenable}
        }
    }

    @Get('/is-full')
    async getUserFull(@Query('username') username: string) {
        const user = await this.UserService.findOne(username);
        if (!user) {
            throw new ForbiddenException('用户不存在');
        }
        const isfull = await this.web3.getUserFull(user.address);
        return {
            statusCode: 200,
            res: {isfull}
        }
    }

    @Get('/range')
    async getUserRange(@Query('username') username: string) {
        const user = await this.UserService.findOne(username);
        if (!user) {
            throw new ForbiddenException('用户不存在');
        }
        const range = await this.web3.getUserRange(user.address);
        return {
            statusCode: 200,
            res: {range}
        }
    }

    //获取用户状态原因消息
    @Get('/message')
    async getUserMe(@Query('username') username: string) {
        const user = await this.UserService.findOne(username);
        if (!user) {
            throw new ForbiddenException('用户不存在');
        }
        const message = await this.web3.getUserMessage(user.address);
        return {
            statusCode: 200,
            res: {message}
        }
    }

    //设置用户权限状态
    @Post('/enable')
    async setUserEnable(@Body() body: {
        username: string, able: boolean, message: string
    }) {
        let {username, able, message} = body;
        // console.log(able)
        // @ts-ignore
        if (able === 'true')
            able = true
        else { // @ts-ignore
            if (able === 'false')
                able = false
        }
        // console.log(able)

        const user = await this.UserService.findOne(username);
        if (!user) {
            throw new ForbiddenException('用户不存在');
        }
        await this.web3.setUserEnable(user.address, able, message);
        return {
            statusCode: 201,
            res: {
                success: "success"
            }
        }
    }

    //设置用户是否已经完整填写信息
    @Post('/full')
    async setUserFull(@Body() body: {
        username: string, isFull: boolean
    }) {
        const {username, isFull} = body;
        const user = await this.UserService.findOne(username);
        if (!user) {
            throw new ForbiddenException('用户不存在');
        }
        await this.web3.setUserFull(user.address, isFull);
        return {
            statusCode: 201,
            res: {
                success: "success"
            }
        }
    }

    //数据库中为修改，设置用户权限等级
    @Post('/range')
    async setUserRange(@Body() body: {
        username: string, modifiedName: string, range: number
    }) {
        const {username, modifiedName, range} = body;
        const user = await this.UserService.findOne(username);
        const moUser = await this.UserService.findOne(modifiedName);
        if (!user || !moUser) {
            throw new ForbiddenException('用户不存在');
        }
        //在数据库中修改权限等级
        await this.web3.setUserRange(user.address, moUser.address, range);
        await this.UserService.setUserRange(moUser, range);
        return {
            statusCode: 201,
            res: {
                success: "success"
            }
        }
    }

//设置用户完整信息
    @Post('/detail')
    async setUserDetail(@Body() body: {
        username: string, userDetail: userdetail
    }) {
        const {username, userDetail} = body;
        const user = await this.UserService.findOne(username);
        if (!user) {
            throw new ForbiddenException('用户不存在');
        }
        //username是不允许修改的
        userDetail.userName = username;
        await this.web3.setUserDetail(user.address, userDetail);
        //在数据库中修改密码
        await this.UserService.setUserPass(user, userDetail.password)
        const newDetail = await this.UserService.getUserInfo(user.address);
        return {
            statusCode: 201,
            res: {
                success: "success",
                newDetail
            }
        }
    }
}
